package com.exam.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.mapper.SelectMapper;
import com.exam.pojo.Question;
import com.exam.pojo.Questionbank;
import com.exam.pojo.Select;
import com.exam.service.QuestionService;
import com.exam.service.QuestionbankService;
import com.exam.vo.QuestionVO;
import com.exam.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/question")
public class QuestionController extends BaseController{
    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionbankService questionbankService;
    @Autowired
    private SelectMapper selectMapper;
    @PostMapping("/set")
    public ResultData<Map<String, Object>> setQuestion(@RequestBody Map<String,Object> questionBody){
        //拆除两个对象
        Object q = questionBody.get("question");
        List select1 = (List) questionBody.get("selectBody");
        //对象转换question
        Question question = JSONObject.parseObject(JSONObject.toJSONString(q), Question.class);
        questionService.getBaseMapper().insert(question);
        System.out.println(question.getId());

        for (Object select2: select1){
            Select select = JSONObject.parseObject(JSONObject.toJSONString(select2), Select.class);
            select.setQuestionId(question.getId());
            selectMapper.insert(select);
            System.out.println(select);
        }

        return  ResultData.success(null);
    }

    @PostMapping("/delete")
    public ResultData<Map<String, Object>> deleteQuestion(@RequestBody List<Integer> deleteIds){
        System.out.println(deleteIds);
        for (Object deleteId : deleteIds) {
            QueryWrapper parperWrapper = new QueryWrapper();
            parperWrapper.eq("id",deleteId);
            questionService.getBaseMapper().delete(parperWrapper);
            QueryWrapper selectWrapper=new QueryWrapper();
            selectWrapper.eq("question_id",deleteId);
            selectMapper.delete(selectWrapper);
        }
        return ResultData.success(null);
    }

    @GetMapping("/getQuestion/{id}")
    public ResultData<Map<String, Object>>getQuestion(@PathVariable("id") String id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Question question = questionService.getBaseMapper().selectOne(queryWrapper);
        queryWrapper.clear();
        queryWrapper.eq("question_id",id);
        List selectList = selectMapper.selectList(queryWrapper);
        question.setQuestionSelect(selectList);
        QuestionVO questionVO = new QuestionVO(question);
        Map map = new HashMap();
        map.put("question",questionVO);
        return new ResultData(map);
    }

    @PostMapping("/update")
    public ResultData<Map<String, Object>>updateQuestion(@RequestBody Map<String,Object> questionBody){
        //拆除两个对象
        Object q = questionBody.get("question");
        List select1 = (List) questionBody.get("selectBody");
        System.out.println("---"+select1);
        //对象转换question
        Question question = JSONObject.parseObject(JSONObject.toJSONString(q), Question.class);
        questionService.getBaseMapper().updateById(question);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("question_id",question.getId());
        selectMapper.delete(queryWrapper);
        for (Object select2: select1){
            Select select = JSONObject.parseObject(JSONObject.toJSONString(select2), Select.class);
            select.setQuestionId(question.getId());
            selectMapper.insert(select);
        }
        return null;
    }

    @GetMapping("/questionBankList")
    public ResultData<Map<String, Object>>questionBankList(@RequestParam(required = false) String keyWord,
                                                           Integer pageNo, Integer pageSize){
        IPage<Questionbank> objectPage = new Page<>(pageNo, pageSize);
        QueryWrapper wrapper = new QueryWrapper();
        if (!Objects.equals(keyWord, null)) {
            wrapper.like("bank_name", keyWord);
        }
        objectPage = questionbankService.page(objectPage, wrapper);
        Map<String, Object> map = new HashMap<>();
        List<Questionbank> questionbanks = objectPage.getRecords();
        for (Questionbank questionbank : questionbanks) {
                wrapper.clear();
                wrapper.eq("question_type",1);
                wrapper.eq("bank_id",questionbank.getBankId());
                questionbank.setSingleCount(questionService.getBaseMapper().selectCount(wrapper));
                wrapper.clear();
                wrapper.eq("question_type",2);
                wrapper.eq("bank_id",questionbank.getBankId());
                questionbank.setMultipleCount(questionService.getBaseMapper().selectCount(wrapper));
                wrapper.clear();
                wrapper.eq("question_type",3);
                wrapper.eq("bank_id",questionbank.getBankId());
                questionbank.setJudgmentCount(questionService.getBaseMapper().selectCount(wrapper));
        }
        map.put("total",objectPage.getTotal());
        map.put("questionBankList",questionbanks);
        return new ResultData(map);
    }
    @PostMapping("/addQuestionBank")
    public ResultData<Map<String, Object>>addQuestionBank(@RequestBody Questionbank questionbank){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bank_name",questionbank.getBankName());
        Questionbank questionbank1 = questionbankService.getBaseMapper().selectOne(queryWrapper);
        if (questionbank1==null){
            int insert = questionbankService.getBaseMapper().insert(questionbank);
            if (insert==1){
                return ResultData.success(null);
            }else {
                return ResultData.error(405,"添加失败");
            }
        }else {
            return ResultData.error(406,"题库名称已被注册,请换一个名称");
        }

    }
    @GetMapping("/deleteQuestionBank/{bankId}")
    public ResultData<Map<String, Object>>deleteQuestionBank(@PathVariable Integer bankId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bank_id",bankId);
        int delete = questionbankService.getBaseMapper().delete(queryWrapper);
        if (delete==1){
            return ResultData.success(null);
        }else{
            return ResultData.error(406,"删除失败");
        }
    }
    @PostMapping("/updateQuestionBank")
    public ResultData<Map<String, Object>>updateQuestionBank(@RequestBody Questionbank questionbank){
        System.out.println(questionbank);
        boolean b = questionbankService.updateById(questionbank);
        if (b){
            return ResultData.success(null);
        }else{
            return ResultData.error(406,"修改失败");
        }
    }
}
